using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Speedball2 {
  public partial class Main : Form {
    private Game _game;
    private static Timer _timer;
    private List<Entity> _dumpPlayers = new List<Entity> ();

    public Main (VersionEnum version) {
      InitializeComponent ();

      SpriteManager spriteManager = new SpriteManager (version);

      _game = new Game ();
      //Width = Width - 320 + _game.Size.Width;
      //Height = Height - 200 + _game.Size.Height;


      _timer = new Timer ();
      _timer.Interval = 1000 / 31;
      _timer.Tick += new EventHandler (_timer_Tick);
      //_timer.Start ();

      _dumpPlayers.Clear ();
      _dumpPlayers.Add (_game._match.PlayersTeam1 [2]);
      //_dumpPlayers.Add (_game._match.EntityBall);
      //_dumpPlayers.Add (_game._match.EntityItemEquipment);

      _stopAtFrame = -1;
      if (Game._debug)
        //_stopAtFrame = 9500; // demo 3
        _stopAtFrame = -1;

      if (_stopAtFrame != -1)
        StepMode = StepModeEnum.FastForward;
      else
        StepMode = StepModeEnum.Normal;
    }
    //private int _stopAtFrame = 9500; // demo 3
    private int _stopAtFrame;

    public static StepModeEnum _stepMode = StepModeEnum.StepByStep;
    private void _timer_Tick (object sender, EventArgs e) {
      if (_stepMode == StepModeEnum.Normal)
        DoUpdate ();
    }

    public static StepModeEnum StepMode {
      get {
        return (_stepMode);
      }
      set {
        _stepMode = value;
        if (_stepMode == StepModeEnum.StepByStep) {
          Match._noDrawing = false;
          _timer.Stop ();
        }
        else if (_stepMode == StepModeEnum.Normal) {
          Match._noDrawing = false;
          _timer.Start ();
        }
        else if (_stepMode == StepModeEnum.FastForward) {
          Match._noDrawing = true;
          _timer.Stop ();
        }
      }
    }

    private void DoUpdate () {
      if (_game._match._frame == (_stopAtFrame - 1))
        StepMode = StepModeEnum.StepByStep;
      if (StepMode == StepModeEnum.FastForward) {
        if (_game._match._frame < _stopAtFrame) {
          int w = (_stopAtFrame - _game._match._frame);
          w = (w * w) / 10000;
          if (w < 10)
            w = 10;
          Match._noDrawing = (_game._match._frame % w) != 0;
        }
        else
          Match._noDrawing = false;
      }
      else
        Match._noDrawing = false;
      if (_game.Update ()) {
        if (StepMode == StepModeEnum.FastForward) {
          StepMode = StepModeEnum.StepByStep;
          DoUpdate ();
        }
        else
          StepMode = StepModeEnum.StepByStep;
      }
      _doubleBufferedPanelUI.Invalidate ();
      //if (_game._match._frame == _stopAtFrame) {
      //  StepMode = StepModeEnum.StepByStep;
      //  //ExtensionMethods.VstClearOutputWindow ();
      //  //_game._match.DumpState (_dumpPlayers);
      //}
      //_dumpPlayers.Add (_game._match.PlayerHavingInputTeam1);
      //_dumpPlayers.Add (_game._match.PlayerHavingInputTeam2);
    }

    private void _doubleBufferedPanelUI_Paint (object sender, PaintEventArgs e) {
      e.Graphics.DrawImage (_game.Screen, 0, 0);
      if (_stepMode == StepModeEnum.FastForward) {
        DoUpdate ();
      }
    }

    private void _doubleBufferedPanelUI_MouseDown (object sender, MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) {
        if (StepMode == StepModeEnum.StepByStep)
          DoUpdate ();
        else
          StepMode = StepModeEnum.StepByStep;
      }
      if (e.Button == MouseButtons.Right) {
        //if ((_stepMode == StepModeEnum.StepByStep) ||
        //    (_stepMode == StepModeEnum.Normal)) {
        //  StepMode = StepModeEnum.FastForward;
        //  DoUpdate ();
        //}
        if (_stepMode == StepModeEnum.StepByStep) {
          StepMode = StepModeEnum.Normal;
        }
        else if (_stepMode == StepModeEnum.Normal) {
          StepMode = StepModeEnum.FastForward;
          DoUpdate ();
        }
        else if (_stepMode == StepModeEnum.FastForward) {
          StepMode = StepModeEnum.Normal;
        }
      }
    }

    private void _doubleBufferedPanelUI_MouseUp (object sender, MouseEventArgs e) {
      //if (e.Button == MouseButtons.Right)
      //  if (StepMode == StepModeEnum.FastForward) {
      //    StepMode = StepModeEnum.Normal;
      //    _timer.Start ();
      //  }
    }

    private void Main_PreviewKeyDown (object sender, PreviewKeyDownEventArgs e) {
      if (_game._match.Bit0_IsComputerPlayer1)
        _game._match.Bit0_IsComputerPlayer1 = false;
      if (e.KeyCode == Keys.Up) {
        _game._match._input1.Bit0_IsUp = true;
        //_game._match._input1.Bit1_IsDown = false;
      }
      if (e.KeyCode == Keys.Down) {
        _game._match._input1.Bit1_IsDown = true;
        //_game._match._input1.Bit0_IsUp = false;
      }
      if (e.KeyCode == Keys.Left) {
        _game._match._input1.Bit2_IsLeft = true;
        //_game._match._input1.Bit3_IsRight = false;
      }
      if (e.KeyCode == Keys.Right) {
        _game._match._input1.Bit3_IsRight = true;
        //_game._match._input1.Bit2_IsLeft = false;
      }
      if (e.KeyCode == Keys.ControlKey)
        _game._match._input1.Bit7_IsButton = true;

      if (e.KeyCode == Keys.Space) {
        ExtensionMethods.VstClearOutputWindow ();
        _game._match.DumpState (_dumpPlayers);
      }
    }

    private void Main_KeyUp (object sender, KeyEventArgs e) {
      if (_game._match.Bit0_IsComputerPlayer1)
        _game._match.Bit0_IsComputerPlayer1 = false;
      //if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down)) {
      //  _game._match._input1.Bit0_IsUp = false;
      //  _game._match._input1.Bit1_IsDown = false;
      //}
      //if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right)) {
      //  _game._match._input1.Bit2_IsLeft = false;
      //  _game._match._input1.Bit3_IsRight = false;
      //}
      if (e.KeyCode == Keys.Up)
        _game._match._input1.Bit0_IsUp = false;
      if (e.KeyCode == Keys.Down)
        _game._match._input1.Bit1_IsDown = false;
      if (e.KeyCode == Keys.Left)
        _game._match._input1.Bit2_IsLeft = false;
      if (e.KeyCode == Keys.Right)
        _game._match._input1.Bit3_IsRight = false;
      if (e.KeyCode == Keys.ControlKey)
        _game._match._input1.Bit7_IsButton = false;
    }
  }

  public enum StepModeEnum {
    Normal,
    StepByStep,
    FastForward
  }
}
